package com.example.dao.impl;

import com.example.dao.MessageRecordDao;
import com.example.entity.customerAdmin.MessageRecord;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MessageRecordDaoImpl implements MessageRecordDao {
    @Override
    public List<MessageRecord> selectAll (int page,int limit) {
        List<MessageRecord> messageRecords=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from messagerecord limit ?,?", objects);
        try {
            while (select.next ()){
                MessageRecord messageRecord=new MessageRecord ();
                messageRecord.setId (select.getInt ("id"));
                messageRecord.setSendUser (select.getString ("sendUser"));
                messageRecord.setSendTime (select.getString ("sendTime"));
                messageRecord.setReceiveUser (select.getString ("receiveUser"));
                messageRecord.setPhone (select.getString ("phone"));
                messageRecord.setMessageContent (select.getString ("messageContent"));
                messageRecords.add (messageRecord);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return messageRecords;
    }

    @Override
    public int del (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        try {
            i=BaseDao.modify ("delste from messagerecord where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from messagerecord ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public MessageRecord selectId (int id) {
        MessageRecord messageRecord=new MessageRecord ();
        Object[] objects=new Object[1];
        objects[1]=id;
        ResultSet select = BaseDao.select ("select * from messagerecord where id=?", objects);
        try {
            while (select.next ()){
                messageRecord.setId (select.getInt ("id"));
                messageRecord.setSendUser (select.getString ("sendUser"));
                messageRecord.setSendTime (select.getString ("sendTime"));
                messageRecord.setReceiveUser (select.getString ("receiveUser"));
                messageRecord.setPhone (select.getString ("phone"));
                messageRecord.setMessageContent (select.getString ("messageContent"));
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return messageRecord;
    }
}
